<?php
namespace App\Services;
use App\Models\CourseModel;
use App\Models\CourseTypeModel;
use App\Models\CourseTagModel;

class CourseService{
    /**
     * 获取分类下课程总数
     * @param int $type_id
     * @return int $course_count
     */
    public function getCourseCount($type_id){
        $course_count = CourseModel::where('tid',$type_id)->count();
        return $course_count;
    }

	/**
	 * 根据标签id获取课程
	 * @param array $tag_id_list
	 * @return $course_list
	 */
	public function getCourseListByTag($tag_id_list){
		$courseTagList = CourseTagModel::whereIn('tag_id',$tag_id_list)->select('course_id')->get()->toArray();

		$course_id = array();
		foreach($courseTagList as $csl=>$cst){
			$course_id[] = $cst['course_id'];
		}
		$course_id = array_merge(array_unique($course_id));
		
		$course_list = CourseModel::where('isshow',1)->whereIn('id',$course_id)->get();
		foreach($course_list as $ctm=>$cts){
			$typeinfo = CourseTypeModel::where('id',$cts->tid)->select('type_name')->first();
			if(!empty($typeinfo)){
				$course_list[$ctm]->type_name = $typeinfo->type_name;
			}else{
				$course_list[$ctm]->type_name = '';
			}
		}

		return $course_list;
	}

	/**
	 * 获取课程总数
	 * @param array $tag_id_list
	 * @return int $courseNum
	 */
	public function getCourseNum($tag_id_list=array(),$isfree = -1){
		if(!empty($tag_id_list)){
			$courseNum = CourseTagModel::whereIn('tag_id',$tag_id_list)->select('id')->count();
		}else{
			$courseNum = CourseModel::where('isshow',1)
						->where(function($query) use ($isfree){
							if($isfree > -1){
								$query->where('isfree',$isfree);
							}
						})
						->select('id')
						->count();
		}
		
		return $courseNum;
	}

	/**
	 * 获取课程列表
	 * @param int $pagesize
	 * @param string $keywords;
	 * @return object $courselist
	 */
	public function getCourseList($pagesize,$keywords = '',$isfree=-1,$type_id=0){
	    if($pagesize <= 0){
            $courselist = CourseModel::orderBy('weight','desc')
                ->where('isshow',1)
                ->where('thumb', '!=', '')
                ->where(function($query) use ($keywords){
                    if($keywords != ''){
                        $query->where('name', 'like', '%'.$keywords.'%');
                    }
                })
                ->where(function($query) use ($isfree){
                    if($isfree > -1){
                        $query->where('isfree',$isfree);
                    }
                })
                ->where(function($query) use ($type_id){
                    if($type_id > 0){
                        $query->where('tid',$type_id);
                    }
                })
                ->get();
        }else{
            $courselist = CourseModel::orderBy('weight','desc')
                ->where('isshow',1)
                ->where(function($query) use ($keywords){
                    if($keywords != ''){
                        $query->where('name', 'like', '%'.$keywords.'%');
                    }
                })
                ->where(function($query) use ($isfree){
                    if($isfree > -1){
                        $query->where('isfree',$isfree);
                    }
                })
                ->paginate($pagesize);
        }

		foreach($courselist as $ctm=>$cst){
			$typeinfo = CourseTypeModel::where('id',$cst->tid)->select('type_name')->first();
			if($typeinfo){
				$courselist[$ctm]->type_name = $typeinfo->type_name;
			}else{
				$courselist[$ctm]->type_name = '';
			}

			unset($typeinfo);
		}
		
		return $courselist;
	}

	/**
	 * 获取课程详细信息
	 * @param string $name
	 * @return object $courseinfo
	 */
	public function getCourseInfoByName($name){
		$courseinfo = CourseModel::where('name',$name)->select('id')->first();
		return $courseinfo;
	}

	/**
	 * 获取课程详细信息
	 * @param int $id
	 * @return object $courseinfo
	 */
	public function getCourseInfoById($id){
		$courseinfo = CourseModel::where('id',$id)->first();
        if(!empty($courseinfo)){
            $ctlist = CourseTagModel::where('course_id',$courseinfo->id)->select('tag_id')->get();
            $tagid_list = array();
            foreach($ctlist as $ctm=>$cst){
                $tagid_list[] = $cst->tag_id;
            }

            $courseinfo->ctlist = $tagid_list;

            return $courseinfo;
        }else{
            return false;
        }

	}

	/**
	 * 插入课程基本信息
	 * @param array $data
	 * @return int $courseid
	 */
	public function insertCourse($data){
		$course = new CourseModel;

		$course->tmid = intval($_POST['tmid']);
		$course->tid = intval($_POST['tid']);
		$course->name = addslashes($_POST['name']);
		$course->desc = addslashes($_POST['desc']);
		$course->price = addslashes($_POST['price']);
		$course->video_num = intval($_POST['videonum']);
		$course->is_buy_one = intval($_POST['isbuy']);
		$course->curriculum_num = intval($_POST['coursenum']);
		$course->status = 0;
		$course->add_date = date('Y-m-d H:i:s');
		$course->isfree = intval($_POST['is_free']);
		$course->isshow = 1;
		$course->save();

		$courseid = $course->id;
		if($courseid){
			if(strpos(addslashes($_POST['tag']),',') !== FALSE){
				$taglist = explode(',',addslashes($_POST['tag']));
				foreach($taglist as $tm=>$ts){
					$tag['tag_id'] = $ts;
					$tag['course_id'] = $courseid;

					CourseTagModel::insert($tag);
				}
			}else{
				$tag['tag_id'] = intval($_POST['tag']);
				$tag['course_id'] = $courseid;

				CourseTagModel::insert($tag);
			}
			
		}
		
		return $courseid;
	}

	/**
	 * 更新课程基本信息
	 * @param int $id
	 * @param array $data
	 * @return bool $res
	 */
	public function modifyCourse($id,$data){
		$res = CourseModel::where('id',$id)->update($data);
		return $res;
	}
}